#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<unordered_map>
#include<string>
using namespace std;
class Solution {
public:
    string minWindow(string s, string t)
    {
        unordered_map<char, int> hash1, hash2;
        for (auto e : t)
        {
            hash1[e]++;
        }
        int left = 0, right = 0, cnt = 0, m = t.size();
        string ret;
        while (right < s.size())
        {
            hash2[s[right]]++;
            if (hash1.count(s[right]) && hash2[s[right]] <= hash1[s[right]])cnt++;
            if (cnt == m)
            {
                while (left < right && (!hash1.count(s[left]) || hash2[s[left]] > hash1[s[left]]))
                {
                    hash2[s[left]]--;
                    if (hash2[s[left]] == 0)hash2.erase(s[left]);
                    left++;
                }
                string tmp = s.substr(left, right - left + 1);
                if (!ret.size()) ret = tmp;
                else ret = tmp.size() < ret.size() ? tmp:ret;
                hash2[s[left]]--;
                if (hash2[s[left]] == 0)hash2.erase(s[left]);
                left++;
                cnt--;
            }
            right++;
        }
        return ret;
    }
};
int main()
{
	string des= "bba";
	string t= "ba";
    Solution().minWindow(des, t);
	return 0;
}